$(function(){
	getdata.init();
	// myLoadUp.loadUpFnContainer();
});
var pagenow = 1;
var m_id = [];
//数据交互
var href = window.location.search;
var moviesinfo = href.substring(1).split("&");
var getdata = {
	init : function(){
		if (moviesinfo[0].indexOf("u_id")==-1) {
			getdata.showMovies();
		}
		else {
			getdata.showRecommendMovie(); 
		}
		getdata.mark();
		getdata.averageScore();
	},
	publish:function(types,uid,mid) {
		var recommendUrl = "/movie/insertcomment.do";
		$(".post").click(function() {
			var score = $(".myscore").text();
			var param = {
				u_id:uid,
				m_id: mid,
				type:types,
				rate:score
			} 
			var judgeClass = $(".scoreshow span").hasClass("myscore");
			if (judgeClass) {
				publicDom.post(recommendUrl,param,function(response){
					response = JSON.parse(response);
					if(response.code == 200){
							layer.msg(response.msg);
						}else{
							layer.msg(response.message);
						}
				},function(err){
						layer.msg('未知错误!');
					})
			}
			else {
				layer.msg("请先评分!");
			}
		});
	},
	showRecommendMovie : function() {
		var recommendUrl = '/movie/getmoviebyid.do';
		if (moviesinfo[1].indexOf("m_id")>-1) {
			var param = {
				m_id : moviesinfo[1].substring(5)
			}
		}
		else {
			var param = {
				m_id : moviesinfo[1]
			}
		}
		var recommendTpl;
		publicDom.post(recommendUrl,param,function(response){
			response = JSON.parse(response);
			if(response.code == 200){
					// for(var i = 0; i < response.object.length; i ++){
						var item = response.object;
						// if(item.m_id==moviesinfo[1]) {
							var starNum = Math.round(item.rate/20);
							$(".img_box img,.bg_blur img").prop("src",item.imgurl);
							$(".prot_whole_name").text("导演:"+item.director);
							// $(".swiper-slide").css("background-image","url("+item.imgurl+")");
							$(".prot_name").text("电影名称:"+item.m_name);
							$(".postage").text("时长:"+item.length+"分钟");
							$(".movieScore .starOut").children().each(function(index,ele){
								index<starNum?$(this).addClass("active_star"):console.log(ele);
							});
							$(".old_price").text("产地:"+item.area);
							$(".sales_volume").text("类型:"+item.type);
							getdata.publish(item.type,u_id,item.m_id);
						// }
					// }
				}else{
					layer.msg(response.message);
				}
		},function(err){
				layer.msg('未知错误!');
			})
	},
	averageScore : function() {
		var showMoviesUrl = '/movie/getcommentByM_id.do';
		if (moviesinfo[1].indexOf("m_id")>-1) {
			var param = {
				m_id : moviesinfo[1].substring(5)
			}
		}
		else {
			var param = {
				m_id : moviesinfo[1]
			}
		}
		publicDom.post(showMoviesUrl,param,function(response){
			response = JSON.parse(response);
			if(response.code == 200){
				var item = response.object.toFixed(2);
				$(".new_price").text(item+"分");	
					
				}else{
					layer.msg(response.message);
				}
		},function(err){
				layer.msg('未知错误!');
			})
	},
	mark : function(){
		$(".star_container .icon-star").on("touchstart", function(event){
			// event.stopPropagation();
			// var ev = ev || window.event;
		 //    var target = ev.target || ev.srcElement;
		 //    if(target.nodeName.toLowerCase() == "i"){
		 //    	target.style.color = "#f90";
		 //    }
			$(event.target).toggleClass("active_star");
			$(event.target).nextAll().removeClass("active_star");
			$(event.target).prevAll().addClass("active_star");
			var index = $(this).index();
			if($(this).hasClass("active_star")){
				$(".scoreshow").html(
									"<span class='myscore'>"
									+(index+1)
									+"</span>"
									+"<span class='myscores'>分</span>"
					);
			}
			else {
				$(".scoreshow").html(
									"<span class='myscore'>"
									+(index)
									+"</span>"
									+"<span class='myscores'>分</span>"
					);
			}
		});
	},
	//显示所有电影接口
	showMovies : function(){
		var showMoviesUrl = '/movie/getmoviebyid.do';
		if (moviesinfo[1].indexOf("m_id")>-1) {
			var param = {
				m_id : moviesinfo[1].substring(5)
			}
		}
		else {
			var param = {
				m_id : moviesinfo[1]
			}
		}
		var liTpl;
		publicDom.post(showMoviesUrl,param,function(response){
			response = JSON.parse(response);
			if(response.code == 200){
					// for(var i = 0; i < response.object.length; i ++){
						var item = response.object;
						// if(item.m_id==moviesinfo[1]) {
							var starNum = Math.round(item.rate/20);
							$(".img_box img,.bg_blur img").prop("src",item.imgurl);
							$(".prot_whole_name").text("导演:"+item.director);
							// $(".swiper-slide").css("background-image","url("+item.imgurl+")");
							$(".prot_name").text("电影名称:"+item.m_name);
							$(".postage").text("时长:"+item.length+"分钟");
							$(".movieScore .starOut").children().each(function(index,ele){
								index<starNum?$(this).addClass("active_star"):console.log(ele);
							});
							$(".old_price").text("产地:"+item.area);
							$(".sales_volume").text("类型:"+item.type);
							var u_id = localStorage.getItem("uid");
							getdata.publish(item.type,u_id,item.m_id);
						// }
					// }
				}else{
					layer.msg(response.message);
				}
		},function(err){
				layer.msg('未知错误!');
			})
	},
}